package cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail.vo.CodeGeneratorDetailPageReqVO;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail.vo.CodeGeneratorDetailRespVO;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorDetail.vo.CodeGeneratorDetailSaveReqVO;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGeneratorDetail.CodeGeneratorDetailDO;
import cn.iocoder.yudao.module.els.service.codeGeneratorDetail.CodeGeneratorDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 编码生成器明细")
@RestController
@RequestMapping("/els/code-generator-detail")
@Validated
public class CodeGeneratorDetailController {

    @Resource
    private CodeGeneratorDetailService codeGeneratorDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建编码生成器明细")
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:create')")
    public CommonResult<Long> createCodeGeneratorDetail(@Valid @RequestBody CodeGeneratorDetailSaveReqVO createReqVO) {
        return success(codeGeneratorDetailService.createCodeGeneratorDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成器明细")
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:update')")
    public CommonResult<Boolean> updateCodeGeneratorDetail(@Valid @RequestBody CodeGeneratorDetailSaveReqVO updateReqVO) {
        codeGeneratorDetailService.updateCodeGeneratorDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成器明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:delete')")
    public CommonResult<Boolean> deleteCodeGeneratorDetail(@RequestParam("id") Long id) {
        codeGeneratorDetailService.deleteCodeGeneratorDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成器明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:query')")
    public CommonResult<CodeGeneratorDetailRespVO> getCodeGeneratorDetail(@RequestParam("id") Long id) {
        CodeGeneratorDetailDO codeGeneratorDetail = codeGeneratorDetailService.getCodeGeneratorDetail(id);
        return success(BeanUtils.toBean(codeGeneratorDetail, CodeGeneratorDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成器明细分页")
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:query')")
    public CommonResult<PageResult<CodeGeneratorDetailRespVO>> getCodeGeneratorDetailPage(@Valid CodeGeneratorDetailPageReqVO pageReqVO) {
        PageResult<CodeGeneratorDetailDO> pageResult = codeGeneratorDetailService.getCodeGeneratorDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeGeneratorDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成器明细 Excel")
    @PreAuthorize("@ss.hasPermission('els:code-generator-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeGeneratorDetailExcel(@Valid CodeGeneratorDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeGeneratorDetailDO> list = codeGeneratorDetailService.getCodeGeneratorDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成器明细.xls", "数据", CodeGeneratorDetailRespVO.class,
                        BeanUtils.toBean(list, CodeGeneratorDetailRespVO.class));
    }

}